package com.android.fragmentdemo
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.android.fragmentdemo.adapters.DynamicItem03Adapter
import com.android.fragmentdemo.adapters.ScrollingLoadItemAdapter
import com.hengfoxwolf.commonbase.base.BaseActivity3
import com.android.fragmentdemo.databeans.DataFactoryIp
import com.android.fragmentdemo.databinding.ActivityScrolling2Binding
import com.android.fragmentdemo.databinding.ActivityScrollingBinding
import com.android.fragmentdemo.defineviews.GridItemDecoration
import com.android.fragmentdemo.defineviews.List01DataItemDecoration
import com.android.fragmentdemo.util.StepLoadCollectionUtil

class Scrolling2Activity : BaseActivity3<ActivityScrolling2Binding>(ActivityScrolling2Binding::inflate) {

    private lateinit var mDyAdapter: ScrollingLoadItemAdapter
    private lateinit var mTotalDataSet: MutableList<String>


    override fun init(savedInstanceState: Bundle?) {
        mTotalDataSet = DataFactoryIp.createSimpleItemData(105)
        StepLoadCollectionUtil.init(mTotalDataSet, 64)
        mDyAdapter = ScrollingLoadItemAdapter()
        mDyAdapter.itemDataSet = StepLoadCollectionUtil.getInitDataSet()

        mBinding.rvScrolling2.apply {
            layoutManager = GridLayoutManager(this@Scrolling2Activity, SPAN_COUNT)
            adapter = mDyAdapter
            addItemDecoration(GridItemDecoration(this@Scrolling2Activity, SPAN_COUNT))

            addOnScrollListener(object : RecyclerView.OnScrollListener() {
                override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                    super.onScrolled(recyclerView, dx, dy)
                    if (recyclerView.layoutManager is GridLayoutManager) {
                        val position =
                            ((recyclerView.layoutManager) as? GridLayoutManager)?.findLastCompletelyVisibleItemPosition() ?: -1
                        //val position2 = ((recyclerView.layoutManager) as? GridLayoutManager)?.findLastVisibleItemPosition()
                        //Log.d(TAG, "position:${position},position2:${position2}")
                        if (mDyAdapter.itemCount - 1 == position && !StepLoadCollectionUtil.hadLoadFinished()) {
                            val mLoadDataSet = StepLoadCollectionUtil.loadItem()
                            if (mLoadDataSet.size > 0) {
                                mDyAdapter.setItemDataSet(mLoadDataSet)
                            }
                        }
                    }
                }

                override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                    super.onScrollStateChanged(recyclerView, newState)
                }
            })
        }
    }

    override fun onStop() {
        super.onStop()
        StepLoadCollectionUtil.reset()
    }

    companion object {
        const val TAG = "Scrolling2Activity"
        const val SPAN_COUNT = 4
    }
}